package com.ubank.transfer;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.ubank.R;
import com.ubank.utils.PublicUtils;

public class TransferAccountActivity extends Activity implements
		OnClickListener
{
	TextView		tag, transfer_type;
	EditText		transfer_amount, transfer_target, account_password;
	Button			next;
	Button			return_btn_baseactivity;
	static double	value				= 0.0;
	String			tagValue, amount, target, password, account;
	boolean			canSubmit			= false;
	boolean			isAmountCorrect		= false;
	boolean			isTargetCorrect		= false;
	boolean			isPasswordCorrect	= false;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.transfer_account);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				R.layout.baseactivity);

		// long start = System.currentTimeMillis();

		tag = (TextView) this.findViewById(R.id.tag_3);
		transfer_type = (TextView) this.findViewById(R.id.transfer_type);

		transfer_amount = (EditText) this.findViewById(R.id.transfer_amount);
		transfer_target = (EditText) this.findViewById(R.id.transfer_target);
		account_password = (EditText) this.findViewById(R.id.account_password);

		next = (Button) this.findViewById(R.id.btn_next);

		Intent intent = this.getIntent();
		tagValue = intent.getStringExtra("tag");
		account = intent.getStringExtra("transferAccount");
		PublicUtils.setTag(tag, tagValue, this, transfer_type);

		next.setOnClickListener(this);
		// PublicUtils.lockScreen(this, start);

		return_btn_baseactivity = (Button) findViewById(R.id.return_btn_baseactivity);
		return_btn_baseactivity.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				finish();
			}
		});
	}

	@Override
	public void onClick(View v)
	{
		amount = transfer_amount.getText().toString().trim();
		target = transfer_target.getText().toString().trim();
		password = account_password.getText().toString().trim();

		isAmountCorrect = PublicUtils.checkTransferAmount(transfer_amount,
				amount, account);

		isTargetCorrect = PublicUtils.checkTransferTarget(transfer_target,
				tagValue, target);

		isPasswordCorrect = PublicUtils.checkAccountPassword(this,
				account_password, account, password);

		if (isAmountCorrect && isTargetCorrect && isPasswordCorrect)
			canSubmit = true;

		if (canSubmit)
		{
			value = Double.parseDouble(amount);
			Intent intent = new Intent(TransferAccountActivity.this,
					TransferConfirmActivity.class);
			intent.putExtra("transferAmount", value);
			intent.putExtra("transferAccount", account);
			intent.putExtra("transferTarget", target);
			intent.putExtra("tag", tag.getText().toString());
			startActivity(intent);
			overridePendingTransition(android.R.anim.slide_in_left,
					android.R.anim.slide_out_right);
			transfer_amount.setText("");
			transfer_target.setText("");
			account_password.setText("");
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		PublicUtils.createMenus(menu);
		PublicUtils.setMenuBackground(this);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		PublicUtils.onMenuItemSelected(this, item);
		return super.onOptionsItemSelected(item);
	}
}
